<template>
  <div class="container">
    <van-sticky>
      <div class="border-b">
        <van-tabs :active="tabActive" @change="onChange" color="#FFE100" line-width="34" :border="false">
          <van-tab title="全部"></van-tab>
          <van-tab title="图文"></van-tab>
          <van-tab title="视频"></van-tab>
        </van-tabs>
      </div>
    </van-sticky>
    <list-item :dataList="dataList"></list-item>
    <!-- 暂无数据 -->
    <div class="align-c" style="margin-top: 120px;" v-if="nodata">
      <img v-if="fileSpace" :src="fileSpace + '/images/shop-nodata.png'" class="width-375 height-91" alt="">
      <div class="align-c mar-t-5 font-16 font-color-6">
        暂无数据
      </div>
    </div>
    <base-line v-if="pullLoad.finished && params.currentPage !== 1"  title='我是有底线滴'></base-line>
  </div>
</template>
<script>
import ListItem from './components/ListItem/index'
import BaseLine from '@/components/BaseLine/index'
import { contentPaging } from '@/api/home'
export default {
  components: { ListItem, BaseLine },
  data() {
    return {
      tabActive: 0,
      dataList: [],
      nodata: false,
      params: {
        size: 10,
        currentPage: 0,
        status: 1,
        enterpriseId: '',
        contentType: '' // ''全部，1图文， 2代表视频
      },
      pullLoad: {
        finished: false,
        loading: false
      }
    }
  },
  onPullDownRefresh() {
    this.nodata = false
    this.params.currentPage = 0
    this.dataList = []
    this.pullLoad.finished = false
    this.init()
    setTimeout(() => {
      this.$pt.stopPullDownRefresh()
    }, 500)
  },
  onReachBottom() {
    // 触底刷新
    if (!this.pullLoad.finished) {
      this.pageInit()
    }
  },
  onLoad(options) {
    // 初始化data
    Object.assign(this.$data, this.$options.data())
  },
  onShow() {
    // 判断从详情页返回，该内容是否是失效的
    const pages = getCurrentPages() // eslint-disable-line
    let curPage = pages[pages.length - 1]
    if (curPage.data.contentInfoId) {
      this.dataList.forEach(item => {
        if (item.contentInfoId === curPage.data.contentInfoId) {
          item.status = 0
        }
      })
      curPage.setData({ contentInfoId: null })
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.params.enterpriseId = this.getEnterpriseId || this.$pt.getStorageSync('appInfo').enterpriseId
      this.params.contentType = this.tabActive === 0 ? '' : this.tabActive
      contentPaging(this.params).then(response => {
        if (response.isError) return
        response.data.records.forEach(item => {
          item.createTime = item.createTime.slice(0, item.createTime.length - 3)
          this.dataList.push(item)
        })
        if (this.dataList.length === 0) {
          this.nodata = true
        }
        this.params.currentPage++
        if (this.dataList.length === 0 || this.dataList.length >= response.data.total) {
          this.pullLoad.finished = true
        }
      }).catch(() => {
      })
    },
    onChange(event) {
      this.tabActive = event.mp.detail.index
      this.nodata = false
      this.params.currentPage = 0
      this.dataList = []
      this.pullLoad.finished = false
      this.init()
    }
  }
}
</script>
<style lang="less" scoped>
.container{
  background-color: #ffffff;
  min-height: 100vh;
}
.border-b{
  border-bottom: 1px solid #E9E9E9;
}
</style>
